/*
Copyright 2014 David Morrissey

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */

package com.raincat.multimediapicker;

import android.graphics.BitmapFactory;
import android.graphics.PointF;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.raincat.multimediapicker.subscaleview.ImageSource;
import com.raincat.multimediapicker.subscaleview.ImageViewState;
import com.raincat.multimediapicker.subscaleview.SubsamplingScaleImageView;
import com.raincat.multimediapicker.utils.ImageItem;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class ViewPagerFragment extends Fragment {

    private static final String BUNDLE_URI = "uri";

    private ImageItem imageItem;
    private OnImageClickListener onClick;

    public interface OnImageClickListener {
        void onClick();
    }

    public ViewPagerFragment() {
    }

    public ViewPagerFragment(OnImageClickListener onClick) {
        this.onClick = onClick;
    }

    public void setImageItem(ImageItem imageItem) {
        this.imageItem = imageItem;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.picker_fragment_visit_item, container, false);

        if (savedInstanceState != null) {
            if (imageItem == null && savedInstanceState.containsKey(BUNDLE_URI)) {
                imageItem = (ImageItem) savedInstanceState.getSerializable(BUNDLE_URI);
            }
        }
        if (imageItem.imagePath != null) {
            if (imageItem.imageSuffix.equalsIgnoreCase("gif")) {
                ImageView imageView = (ImageView) rootView.findViewById(R.id.ImageView);
                Glide.with(getActivity()).asGif().load(imageItem.imagePath).diskCacheStrategy(DiskCacheStrategy.NONE).into(imageView);
                imageView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onClick != null)
                            onClick.onClick();
                    }
                });
            } else if (imageItem.outHeight == 0 || imageItem.outWidth == 0) {
                TextView error = (TextView) rootView.findViewById(R.id.tv_error);
                error.setVisibility(View.VISIBLE);
            } else {
                float scale = getInitImageScale(imageItem.imagePath);
                SubsamplingScaleImageView imageView = (SubsamplingScaleImageView) rootView.findViewById(R.id.SubsamplingScaleImageView);
                imageView.setMaxScale(scale + 2.0f);
                imageView.setImage(ImageSource.uri(imageItem.imagePath), new ImageViewState(scale, new PointF(0, 0), 0));
                imageView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onClick != null)
                            onClick.onClick();
                    }
                });
            }
        }

        return rootView;
    }

    public float getInitImageScale(String imagePath) {
        float scale = 1.0f;
        try {
            BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(imagePath)));
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(in, null, options);
            int width = ((PickerActivity) getActivity()).getPhoneW();
            // 拿到图片的宽和高
            int dw = options.outWidth;
            scale = width * 1.0f / dw;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return scale;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        View rootView = getView();
        if (rootView != null) {
            outState.putSerializable(BUNDLE_URI, imageItem);
        }
    }
}
